package ronin.util;

//import ronin.debug.*;

public class MagicUtils extends Object {
    // pre-calculated values for sin, cos, and tan values

    static double[] sinTable = null;
    static double[] cosTable = null;
    static double[] tanTable = null;

    // builds the three trig lookup tables
    public static void buildTrigTables() {
	// Debugger.reportMessage("Building trig tables
	// (MagicUtils.buildTrigTables)...");

	sinTable = new double[360];
	cosTable = new double[360];
	tanTable = new double[360];

	double radians;
	for (int i = 0; i < 360; i++) {
	    radians = Math.toRadians(i);
	    sinTable[i] = Math.sin(radians);
	    cosTable[i] = Math.cos(radians);
	    tanTable[i] = Math.tan(radians);
	}

    }

    public static double cos(int degrees) {
	if (degrees < 0) {
	    degrees = 360 + degrees % 360;
	}
	if (degrees >= 360) {
	    degrees %= 360;
	}

	try {
	    return cosTable[degrees];
	} catch (NullPointerException e) {
	    buildTrigTables();
	    return cos(degrees);
	}
    }

    public static double sin(int degrees) {
	if (degrees < 0) {
	    degrees = 360 + degrees % 360;
	}
	if (degrees >= 360) {
	    degrees %= 360;
	}

	try {
	    return sinTable[degrees];
	} catch (NullPointerException e) {
	    buildTrigTables();
	    return sin(degrees);
	}
    }

    public static double tan(int degrees) {
	if (degrees < 0) {
	    degrees = 360 + degrees % 360;
	}
	if (degrees >= 360) {
	    degrees %= 360;
	}

	try {
	    return tanTable[degrees];
	} catch (NullPointerException e) {
	    buildTrigTables();
	    return tan(degrees);
	}
    }
    
    public static double randInRange(double x, double y){
    	return x + Math.random()*(y-x);
    }

} // MagicUtils
